package com.lhj.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhj.reggie.common.R;
import com.lhj.reggie.dto.DishDto;
import com.lhj.reggie.entity.Dish;
import com.lhj.reggie.service.DishFlavorService;
import com.lhj.reggie.service.DishService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/dish")
public class DishController {

    @Resource
    private DishService dishService;

    @Resource
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品，同时保留对应的口味数据
     *
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        return dishService.savewithFlavor(dishDto);
    }

    /**
     * 菜品分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        return dishService.page(page, pageSize, name);
    }

    /**
     * 根据id查询菜品信息和对应的口味
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id) {
        DishDto dishDto = dishService.getbyidWithFlavor(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品
     *
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {
        return dishService.updatewithFlavor(dishDto);
    }

    /**
     * 根据id查询分类菜品信息
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        return dishService.list(dish);
    }
}
